from django.db import models
from django.utils.html import format_html
# Create your models here.
class Members(models.Model):
    employee = models.ForeignKey('Employees', on_delete=models.SET_NULL, null=True, verbose_name='客户经理')
    openid = models.CharField(max_length=32)
    name = models.CharField('姓名', max_length=10)
    mobile = models.CharField('手机号', max_length=11)
    wxStatus = models.IntegerField()
    status = models.IntegerField('状态')
    created_at = models.DateTimeField(auto_now_add=True)

    def wx_colored(self):
        if self.wxStatus == 0:
            wx_color = 'red'
            wx_text = '未关注'
        elif self.wxStatus == 1:
            wx_color = 'green'
            wx_text = '已关注'
        elif self.wxStatus == -1:
            wx_text = '取消关注'
            wx_color = 'yellow'
        return format_html(
            '<span style="color: {};">{}</span>',
            wx_color,
            wx_text
        )
    wx_colored.short_description = '关注状态'

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'members'
        verbose_name_plural = '会员'

class Cards(models.Model):
    card_id = models.CharField(max_length=32)
    card_name = models.CharField('会员卡名', max_length=32)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.card_name

    class Meta:
        db_table = 'cards'
        verbose_name_plural = '会员卡'

class MemberCards(models.Model):
    member = models.ForeignKey('Members', on_delete=models.SET_NULL, null=True, verbose_name='会员')
    card = models.ForeignKey('Cards', on_delete=models.SET_NULL, null=True, verbose_name='会员卡')
    card_code = models.CharField('会员卡号', max_length=15)
    card_status = models.IntegerField('状态')
    created_at = models.DateTimeField(auto_now_add=True)

    def status_colored(self):
        if self.card_status == 1:
            color = 'green'
            text = '激活'
        elif self.card_status == 0:
            color = 'royalblue'
            text = '领卡'
        else:
            color = 'red'
            text = '删除'
        return format_html(
            '<span style="color: {};">{}</span>',
            color,
            text,
        )
    status_colored.short_description = '卡状态'

    class Meta:
        db_table = 'member_cards'
        verbose_name_plural = '会员卡发放'

class Employees(models.Model):
    userid = models.CharField(max_length=100)
    name = models.CharField("姓名", max_length=10)
    mobile = models.CharField("手机号", max_length=11)
    email = models.EmailField()
    deviceid = models.CharField(max_length=100)
    qrcode = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)

    def qrcode_colored(self):
        if self.qrcode == None:
            return format_html('<a href="/member/wx/qrcode/add/{}/">申请二维码</a>', self.id)
        else:
            return format_html('<span style="color: red;">已申请</span>')
    qrcode_colored.short_description = '二维码'

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'employees'
        verbose_name_plural = '雇员'

class System(models.Model):
    name = models.CharField('名称', max_length=20)
    content = models.CharField('内容', max_length=255)

    class Meta:
        db_table = 'system'
        verbose_name_plural = '系统设置'